﻿///////////////////////////////////////////////////////////////////////////////
// File:        SelectReactor.hpp
// Author:      671643387@qq.com
// Description:
///////////////////////////////////////////////////////////////////////////////

#ifndef NIO_NET_DETAIL_SELECTREACTOR_HPP_
#define NIO_NET_DETAIL_SELECTREACTOR_HPP_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <boost/unordered_map.hpp>
#include <nio/net/Reactor.hpp>

namespace nio
{
    namespace net
    {
        namespace detail
        {
            class SelectReactor : public Reactor
            {
            public:
                SelectReactor(void);
                virtual ~SelectReactor(void);

            public:
                virtual bool addSource(sock_t sock, Event events, EventHandler* handler);
                virtual void removeSource(sock_t sock);
                virtual void doTick(base::time_t timeout = 0);


            private:
                ///////////////////////////////////////////////////////////////
                enum FDArray
                {
                    BAK = 0,
                    USE,
                    SUM
                };

                typedef boost::unordered_map<sock_t, EventHandler*> SourceMap;
                ///////////////////////////////////////////////////////////////
            private:
                nio::base::time_t   lastHeartbeatTime_;

                ::fd_set            readFds_[SUM];
                ::fd_set            writeFds_[SUM];
                ::fd_set            errorFds_[SUM];

                SourceMap           sources_;
            };
        }
    }
}

#endif //NIO_NET_DETAIL_SELECTREACTOR_HPP_
